<?php
                                                                                                                                                                                                                                                                                                                                        
// TTTTTTTTTTTTTTTTTTTTTTT                  MMMMMMMM               MMMMMMMM                                CCCCCCCCCCCCMMMMMMMM               MMMMMMMM  SSSSSSSSSSSSSSS 
// T:::::::::::::::::::::T                  M:::::::M             M:::::::M                             CCC::::::::::::M:::::::M             M:::::::MSS:::::::::::::::S
// T:::::::::::::::::::::T                  M::::::::M           M::::::::M                           CC:::::::::::::::M::::::::M           M::::::::S:::::SSSSSS::::::S
// T:::::TT:::::::TT:::::T                  M:::::::::M         M:::::::::M                          C:::::CCCCCCCC::::M:::::::::M         M:::::::::S:::::S     SSSSSSS
// TTTTTT  T:::::T  TTTTTppppp   ppppppppp  M::::::::::M       M::::::::::M   eeeeeeeeeeee          C:::::C       CCCCCM::::::::::M       M::::::::::S:::::S            
//         T:::::T       p::::ppp:::::::::p M:::::::::::M     M:::::::::::M ee::::::::::::ee       C:::::C             M:::::::::::M     M:::::::::::S:::::S            
//         T:::::T       p:::::::::::::::::pM:::::::M::::M   M::::M:::::::Me::::::eeeee:::::ee     C:::::C             M:::::::M::::M   M::::M:::::::MS::::SSSS         
//         T:::::T       pp::::::ppppp::::::M::::::M M::::M M::::M M::::::e::::::e     e:::::e     C:::::C             M::::::M M::::M M::::M M::::::M SS::::::SSSSS    
//         T:::::T        p:::::p     p:::::M::::::M  M::::M::::M  M::::::e:::::::eeeee::::::e     C:::::C             M::::::M  M::::M::::M  M::::::M   SSS::::::::SS  
//         T:::::T        p:::::p     p:::::M::::::M   M:::::::M   M::::::e:::::::::::::::::e      C:::::C             M::::::M   M:::::::M   M::::::M      SSSSSS::::S 
//         T:::::T        p:::::p     p:::::M::::::M    M:::::M    M::::::e::::::eeeeeeeeeee       C:::::C             M::::::M    M:::::M    M::::::M           S:::::S
//         T:::::T        p:::::p    p::::::M::::::M     MMMMM     M::::::e:::::::e                 C:::::C       CCCCCM::::::M     MMMMM     M::::::M           S:::::S
//       TT:::::::TT      p:::::ppppp:::::::M::::::M               M::::::e::::::::e                 C:::::CCCCCCCC::::M::::::M               M::::::SSSSSSS     S:::::S
//       T:::::::::T      p::::::::::::::::pM::::::M               M::::::Me::::::::eeeeeeee          CC:::::::::::::::M::::::M               M::::::S::::::SSSSSS:::::S
//       T:::::::::T      p::::::::::::::pp M::::::M               M::::::M ee:::::::::::::e            CCC::::::::::::M::::::M               M::::::S:::::::::::::::SS 
//       TTTTTTTTTTT      p::::::pppppppp   MMMMMMMM               MMMMMMMM   eeeeeeeeeeeeee               CCCCCCCCCCCCMMMMMMMM               MMMMMMMMSSSSSSSSSSSSSSS   
//                        p:::::p                                                                                                                                       
//                        p:::::p                                                                                                                                       
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       ppppppppp                                                                                                                                      
                                                                                                                                                                     
//  _____      __  __         ____ __  __ ____  
// |_   __ __ |  \/  | ___   / ___|  \/  / ___|     | AUTHOR: Xiaohe
//   | || '_ \| |\/| |/ _ \ | |   | |\/| \___ \     | EMAIL: 496631085@qq.com
//   | || |_) | |  | |  __/ | |___| |  | |___) |    | WECHAT: he4966
//   |_|| .__/|_|  |_|\___|  \____|_|  |_|____/     | DATETIME: 2021/07/27
//      |_|                                         | TpMeCMS

namespace app\api\controller;
use app\api\controller\Tpmecms;
use think\Db;
use think\Config;
/**
 * 公共无需登陆接口
 */
class Com extends Tpmecms
{

    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];



    /**
     * 获取配置🏘
     *
     * @param string $field 获取需要的字段例如name（可不传获取全部
     * @return void
     */
    public function get_config($field = null)
    {
        if($field){
            $this->success($field,Config::get('site.'.$field));
        }else{
            $this->success('all',Config::get('site'));
        }
    }

    /**
     * 获取banner🏘
     *
     * @param string $status 类型:1=首页区,2=广告区
     * @return void
     */
    public function get_banner($status='1')
    {
        $where['status'] = ['=',$status];

        $this->success('ok',Db::name('banner')
                ->where($where)
                ->order('weigh desc')
                ->select());
    }

    /**
     * 普通用户获取推荐列表
     * @param string $lng 经度
     * @param string $lat 纬度
     * @return void
     */
    public function get_hot_goods_list($lng=null,$lat=null)
    {
        $list = Db::name('goods')
            ->where('siteswitch','1')
            ->where('hot_switch','1')
            ->order('weigh desc')
            ->select();
        $goodscate = Db::name('goodscate')->column('*','id');
        if($list){
            foreach($list as &$val){
                $val['room_list'] = Db::name('room')->where('goods_id',$val['id'])->select();
                if($lng && $lat){
                    $val['km'] = rand(1,9999);
                    $geo = new \addons\redisgeo\controller\Index();

                }else{
                    $val['km'] = 0;
                }
                $val['cate_name'] = $goodscate[$val['goodscate_id']]['cate_name'];
                
            }
        }
        $this->success('ok',$list);
    }

    /**
     * 获取房源信息🏘
     *
     * @param int $goods_id 房源id
     * @return void
     */
    public function get_goods_data($goods_id)
    {
        $data = $this->GetGoodsData($goods_id);
        $this->AddGoodsLookLog($goods_id,$this->auth->id);
        $this->success('ok',$data);
    }

    /**
     * 获取房源分类🏘
     *
     * @return void
     */
    public function get_goodscate()
    {
        
        $list = Db::name('goodscate')->order('weigh desc')->select();
        $this->success('ok',$list);
    }

    /**
     * 获取公司列表🏘
     *
     * @return void
     */
    public function get_ltd_list()
    {
        
        $list = Db::name('ltd')->where('switch',1)->select();
        $this->success('ok',$list);
    }

    /**
     * 获取房产咨讯栏目列表🏘
     *
     * @return void
     */
    public function get_artcate_list()
    {
        
        $list = Db::name('artcate')->select();
        $this->success('ok',$list);
    }



    /**
     * 搜索房源🕍
     *
     * @param int $page 1
     * @param int $limit 5
     * @param float $lng 经度（可不传
     * @param float $lat 纬度（可不传
     * @param string $name 搜索名称(可不传
     * @param string $price_sort 价格排序（ 默认 nullASC升序或DESC降序
     * @param string $distance_sort 距离排序（默认null——ASC升序或DESC降序
     * @param string $house_type 户型
     * @param string $province 省
     * @param string $city 市
     * @param string $district 区
     * @param string $now_status 现房:1=现房,2=准现房,3=新房,4=商铺,5=租房,6=二手房
     * @param string $area 区域
     * @param string $tag_id 标签
     * @return void
     */
    public function search_goods_list($tag_id = null,$area = null,$page = 1,$limit = 5,$now_status = null,$province=null ,$city=null , $district=null ,$house_type=null ,$lng=null,$lat=null,$name=null,$de_xyj=null,$de_kt=null,$de_xwj=null,$de_wbl=null,$de_bx=null,$price_sort = null,$distance_sort=null,$s_time = null,$goodscate_id = NULL)
    {
       
        // $geo = new \addons\redisgeo\controller\Index();
        $where = null;
        $where_tag = null;
        $where['public_status'] = ['=','1'];//公开显示:1=是,2=否
        $where['siteswitch'] = ['=','1'];

        if($name){
            $where['name'] = ['like','%'.$name.'%'];
        }

        if($house_type){
            $where['house_type'] = ['like','%'.$house_type.'%'];
        }
        
        if($area){ //区域
            $where['area'] = ['=',$area];
        }
        if($tag_id){
            // $where['tag_ids'] = ['=',$tag_id];
            // $where['exp']=['FIND_IN_SET('.$tag_id.',tag_ids)'];
            $where_tag = 'find_in_set('.$tag_id.',tag_ids)';
        }
        

        // if($goodscate_id){
        //     $where['goodscate_id'] = ['=',$goodscate_id];
        // }
        //————————————————————————————————————————————————————
        // if($zb_sc){ //周边市场
        //     $where['zb_sc'] = ['=','有'];
        // }
        // if($zb_school){ //周边学校
        //     $where['zb_school'] = ['=','有'];
        // }
        // if($zb_hospital){ //周边医院
        //     $where['zb_hospital'] = ['=','有'];
        // }
        // if($zb_bus){ //周边公交
        //     $where['zb_bus'] = ['=','有'];
        // }
        // if($de_kzf){ //可做饭
        //     $where['de_kzf'] = ['=','有'];
        // }
        // if($de_kt){ //空调
        //     $where['de_kt'] = ['=','有'];
        // }
        // if($de_xyj){ //洗衣机
        //     $where['de_xyj'] = ['=','有'];
        // }
        // if($de_furniture){ //有无家具
        //     $where['de_furniture'] = ['=','有'];
        // }
        // if($de_bathroom){ //有无卫浴
        //     $where['de_bathroom'] = ['=','有'];
        // }
        // if($de_tv){ //有无电视
        //     $where['de_tv'] = ['=','有'];
        // }
        // if($de_wifi){ //有无wifi
        //     $where['de_wifi'] = ['=','有'];
        // }
        //----------------------------------------
        if($now_status){ //现房:1=现房,2=准现房,3=新房,4=商铺,5=租房,6=二手房
            $where['now_status'] = ['=',$now_status];
        }

   
        if($province){ //省
            $where['province'] = ['=',$province];
        }

        if($city){ //市
            $where['city'] = ['=',$city];
        }

        if($district){ //区
            $where['district'] = ['=',$district];
        }

    //    halt($where);
       
        // $goodscate = Db::name('goodscate')->column('*','id');
        
        if($price_sort){
                //用价格排序
                $list = Db::name('goods')
                        ->where($where)
                        ->where($where_tag)
                        ->order('price '.$price_sort)
                        ->page($page,$limit)
                        
                        ->select();
                if($list){
                    foreach ($list as $key => &$val) {
                        // $val['room_list'] = Db::name('room')->where('goods_id',$val['id'])->select();
                        // $val['cate_name'] = $goodscate[$val['goodscate_id']]['cate_name'];
                        // $val['km'] = $geo->get_coordinate_distance($lng,$lat,$val['lng'],$val['lat']);
                    }
                }
        }elseif($distance_sort){
            $geo_list = $geo->get_nearby_list($lng,$lat,99999999999,'m',10000,$distance_sort);
            $start=($page-1)*$limit;//当前页-1乘以每页显示条数
            $geo_list = array_slice($geo_list,$start,$limit);//取数组一段值  数组 开始 数量 保留键名默认false

            $list = array();
           
            $start=($page-1)*$limit;//当前页-1乘以每页显示条数
            //有个 bug 这是按先查询后的店铺再获取的附近的  其实最近的不再最前面 
            foreach ($geo_list as $key => $geo) {
                
                  


                    $temp = Db::name('goods')->where('id',$geo[0])->find();
                    if($temp){
                        // $temp['km'] = round($geo[1]/1000,2);
                        // $temp['room_list'] = Db::name('room')->where('goods_id',$temp['id'])->select();
                        // $temp['cate_name'] = $goodscate[$temp['goodscate_id']]['cate_name'];                        
                        $list[] = $temp;
                    }
            }  
            $list = array_slice($list,$start,$limit);//取数组一段值  数组 开始 数量 保留键名默认false

        
        

            $this->success('ok',$list);
        }else{
            //没有排序
            $list = Db::name('goods')
                    ->where($where)
                    ->where($where_tag)
                    ->page($page,$limit)
                    ->select();
            if($list){
                foreach ($list as $key => &$val) {
                    // $val['room_list'] = Db::name('room')->where('goods_id',$val['id'])->select();
                    // $val['cate_name'] = $goodscate[$val['goodscate_id']]['cate_name'];
                    // $val['km'] = $geo->get_coordinate_distance($lng,$lat,$val['lng'],$val['lat']);
                }
            }
        } 

        if($list){
            foreach ($list as $key => &$val) {
                // $val['room_list'] = Db::name('room')->where('goods_id',$val['id'])->select();
                $val['tag'] = Db::name('tag')->where('id','in',$val['tag_ids'])->column('title');
                // $val['km'] = $geo->get_coordinate_distance($lng,$lat,$val['lng'],$val['lat']);
            }
        }

        $this->success('ok',$list);

    }




    /**
     * 获取房源列表🏛
     * @param int $page 1
     * @param int $limit 10
     * @param string $now_status 现房:1=现房,2=准现房,3=新房,4=商铺,5=租房,6=二手房
     * @param string $hot_switch 热门推荐
     * @return void
     */
    public function get_goods_list($now_status = null,$page=1,$limit=10,$hot_switch=null)
    {
        // $where['uid'] = $this->auth->id;
        $where = null;
        if($now_status){
            $where['now_status'] = ['=',$now_status];
        }
        if($hot_switch){
            $where['hot_switch'] = ['=','1'];
        }
        $list = Db::name('goods')
                    ->where($where)
                    ->order('time desc')
                    
                    ->page($page,$limit)
                    ->select();
        foreach ($list as $key => &$val) {
            $val['tags'] = Db::name('tag')->where('id','in',$val['tag_ids'])->column('title');
        }
                    
        $this->success('one',$list);
    }

    /**
     * 获取标签列表⌨️
     *
     * @return void
     */
    public function get_tag_list()
    {
        $list = Db::name('tag')->order('weigh desc')->select();
        $this->success('ok',$list);
    }


    /**
     * 获取房产资讯列表📃
     *
     * @param integer $page 1
     * @param integer $limit 10
     * @param int $artcate_id 资讯分类;
     * @param string $search 搜索内容
     * @return void
     */
    public function get_art_list($page=1,$limit=10,$artcate_id=null,$search=null)
    {
        $where = null;
        $wherelike1 = null;
        $wherelike2 = null;
        if($artcate_id)
            $where['artcate_id'] = $artcate_id;
        if($search){
            
            $wherelike1['title'] = ['like','%'.$search.'%'];
            $wherelike2['content'] = ['like','%'.$search.'%'];
        }
        $list = Db::name('art')
                ->order('weigh DESC')
                ->where($where)
                ->where($wherelike1)
                ->whereOr($wherelike2)
                ->page($page,$limit)

                ->field([ '*,FROM_UNIXTIME(createtime,"%Y-%m-%d %H:%i:%s") as date'])
                ->select();

        $this->success('ok',$list);
    }


    /**
     * 获取房产资讯信息📋
     *
     * @param int $id id
     * @return void
     */
    public function get_art_data($id)
    {
        $data['id'] = $id;
        $res = Db::name('art')
            ->where($data)
            ->field([ '*,FROM_UNIXTIME(createtime,"%Y-%m-%d %H:%i:%s") as date'])
            ->find();
        if($res)$this->AddArtView($id);
        $this->success('ok',$res);
    }



    /**
     * 添加预约看房🏚
     * @ApiSummary ("0=待处理1=未联系上2=已联系上3=预定成功4=暂无需求5=完结")
     * @param string $name 姓名
     * @param string $tel 电话
     * @param string $goods_id 房源id
     * @param string $is_status 是否接送默认1，1=是，2=否
     * @param string $appoint_time 预约时间
     * @return void
     */
    public function add_look_house()
    {
        $params = $this->request->param();
        // halt($params);
        $fields = ['name','tel','goods_id','is_status','appoint_time'];
        $data = $this->get_field_arr($params,$fields,true);
        // halt($data);

        $data['admin_id'] = $this->GetGoodsData($data['goods_id'])['admin_id'];
        $data['uid'] = $this->auth->id;
        $res = Db::name('look_house')->where($data)->find();
        
        
        if($res){
            $this->error('已有该需求');
        }else{
            $data['status'] = '0';
            $data['time'] = time();
            $id = Db::name('look_house')->insertGetId($data);

            if($id)$this->success('提交成功');

        }
    }

}
